# -*- coding: utf-8 -*-
import json

import datetime
import pymongo

import time

from NetEaseSpider.spiders.comment import comment

client = pymongo.MongoClient('175.102.18.112', 27018)

db_comment = client.wuliao.comment
db_song = client.wuliao.song
db_user = client.wuliao.user


def saveUser(user):
    db_item = db_user.find({'userId': user['userId']})
    if db_item.count() == 0:
        db_user.save(user)


def saveComment(data, songId):
    for i in data['comments']:
        # 判断commentId
        item = {}
        reply = {}

        item['content'] = i['content']
        item['songId'] = songId
        item['userId'] = i['user']['userId']
        item['likeCount'] = i['likedCount']
        item['commentId'] = i['commentId']
        item['time'] = i['time']
        item['tag'] = 'song'
        item['createdTime'] = long(time.time()) * 1000

        if (len(i['beReplied']) > 0):
            reply['content'] = i['beReplied'][0]['content']
            reply['userId'] = i['beReplied'][0]['user']['userId']
            reply['nickname'] = i['beReplied'][0]['user']['nickname']

            item['beReplied'] = reply
        user = i['user']
        user.pop('remarkName')
        saveUser(user)

        db_item = db_comment.find({'commentId': item['commentId']})

        try:
            if db_item.count() == 0:
                db_comment.save(item)

        except Exception:
            print "exception" + item['commentId']

    time.sleep(2)

def saveHotComment(data, songId):
    for i in data['hotComments']:
        # 判断commentId
        item = {}
        reply = {}

        item['content'] = i['content']
        item['songId'] = songId
        item['userId'] = i['user']['userId']
        item['likeCount'] = i['likedCount']
        item['commentId'] = i['commentId']
        item['time'] = i['time']
        item['tag'] = 'song'
        item['createdTime'] = long(time.time()) * 1000

        if (len(i['beReplied']) > 0):
            reply['content'] = i['beReplied'][0]['content']
            reply['userId'] = i['beReplied'][0]['user']['userId']
            reply['nickname'] = i['beReplied'][0]['user']['nickname']

            item['beReplied'] = reply
        user = i['user']
        user.pop('remarkName')
        saveUser(user)

        db_item = db_comment.find({'commentId': item['commentId']})

        try:
            if db_item.count() == 0:
                db_comment.save(item)

        except Exception:
            print "exception" + item['commentId']



def start():
    for i in db_song.find():
        print str(i['id'])
        page = 0

        last_data = ""

        data = json.loads(comment(str(i['id']), page).get_data())
        saveHotComment(data,str(i['id']))
        # saveComment(data, str(i['id']))
        #
        # while (str(data['comments']) != last_data):
        #     page = page + 1
        #     last_data = str(data['comments'])
        #     data = json.loads(comment(str(i['id']), page).get_data())
        #     saveComment(data, str(i['id']))


start()
